package com.lx.es.query.test.service;

import cn.lx.es.query.search.entity.PageList;
import cn.lx.es.query.search.entity.PageSortHighLight;
import cn.lx.es.query.search.entity.SearchParam;
import cn.lx.es.query.search.entity.Sort;
import cn.lx.es.query.search.entity.HighLight;
import org.elasticsearch.search.sort.SortOrder;
import cn.lx.es.query.search.entity.Range;
import cn.lx.es.query.search.service.EsSearchService;
import java.math.BigDecimal;
import com.lx.es.query.test.dto.DrugSearchDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 药品搜索服务
 *
 * @author test
 * @date 2025-01-02
 */
@Service
public class DrugSearchService {

    @Autowired
    private EsSearchService<DrugSearchDTO> esSearchService;

    /**
     * 基础搜索示例
     */
    public PageList<DrugSearchDTO> searchDrugs(DrugSearchDTO searchDTO) {
        // 设置分页
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(10);

        // 执行搜索
        SearchParam<DrugSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, DrugSearchDTO.class);
    }

    /**
     * 按药品名称搜索
     */
    public PageList<DrugSearchDTO> searchByDrugName(String drugName) {
        DrugSearchDTO searchDTO = new DrugSearchDTO();
        searchDTO.setDrugName(drugName);
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(20);

        SearchParam<DrugSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, DrugSearchDTO.class);
    }

    /**
     * 按生产企业搜索
     */
    public PageList<DrugSearchDTO> searchByManufacturer(String manufacturer) {
        DrugSearchDTO searchDTO = new DrugSearchDTO();
        searchDTO.setProdentpName(manufacturer);
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(20);

        SearchParam<DrugSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, DrugSearchDTO.class);
    }

    /**
     * 按剂型搜索
     */
    public PageList<DrugSearchDTO> searchByDosageForm(String dosageForm) {
        DrugSearchDTO searchDTO = new DrugSearchDTO();
        searchDTO.setDosformName(dosageForm);
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(20);

        SearchParam<DrugSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, DrugSearchDTO.class);
    }

    /**
     * 按药理分类搜索（多值匹配）
     */
    public PageList<DrugSearchDTO> searchByPhamType(String phamTypes) {
        DrugSearchDTO searchDTO = new DrugSearchDTO();
        searchDTO.setPhamTypeName1(phamTypes); // 支持逗号分隔: "抗生素,抗病毒"
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(20);

        SearchParam<DrugSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, DrugSearchDTO.class);
    }

    /**
     * 按日均使用量范围搜索
     */
    public PageList<DrugSearchDTO> searchByDayUseRange(Double minDayUse, Double maxDayUse) {
        DrugSearchDTO searchDTO = new DrugSearchDTO();
        searchDTO.setDayuse(maxDayUse.toString()+","+minDayUse);
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(20);

        SearchParam<DrugSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, DrugSearchDTO.class);
    }

    /**
     * 复杂搜索示例：按药品名称、生产企业、剂型组合搜索
     */
    public PageList<DrugSearchDTO> complexSearch(String drugName, String manufacturer, String dosageForm) {
        DrugSearchDTO searchDTO = new DrugSearchDTO();
        searchDTO.setDrugName(drugName);
        searchDTO.setProdentpName(manufacturer);
        searchDTO.setDosformName(dosageForm);
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(20);

        // 设置排序
        Sort sort = new Sort(new Sort.Order(SortOrder.ASC, "drugName"));
        searchDTO.setSort(sort);

        // 设置高亮
        HighLight highLight = new HighLight();
        highLight.field("drugName").field("prodentpName");
        highLight.setPreTag("<em>");
        highLight.setPostTag("</em>");
        searchDTO.setHighLight(highLight);

        SearchParam<DrugSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, DrugSearchDTO.class);
    }

    /**
     * 跨索引搜索示例
     */
    public PageList<DrugSearchDTO> crossIndexSearch(String drugName, String... indices) {
        DrugSearchDTO searchDTO = new DrugSearchDTO();
        searchDTO.setDrugName(drugName);
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(20);

        SearchParam<DrugSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, DrugSearchDTO.class, indices);
    }

    /**
     * 搜索基药
     */
    public PageList<DrugSearchDTO> searchEssentialDrugs() {
        DrugSearchDTO searchDTO = new DrugSearchDTO();
        searchDTO.setEssdrugFlag("1"); // 假设1表示基药
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(50);

        SearchParam<DrugSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, DrugSearchDTO.class);
    }

    /**
     * 搜索医保药品
     */
    public PageList<DrugSearchDTO> searchMedicalInsuranceDrugs() {
        DrugSearchDTO searchDTO = new DrugSearchDTO();
        searchDTO.setHiType("1"); // 假设1表示医保药品
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(50);

        SearchParam<DrugSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, DrugSearchDTO.class);
    }
}
